# coding: UTF-8
# Filename : version.py
# Company : imedia
# Author : zqz # -*-coding:utf-8 -*-
from api.common.common import *
from api.common import mysql,log,utils,httputils,const,xml_tpl, err_tpl
import version_data


'''
此接口为版本类型映射
传入app_type就是 os参数
'''

#入口函数
def mapping(request):
    #record log((frist action))
    httputils.footprint(request)

    if validate(request):
        return validate(request)

    #receive param
    app_type = httputils.post(request, 'os')
    cus_id = httputils.post(request, 'cus_id')

    #当前系统 0为android，1为iphone
    if not utils.is_num(app_type): app_type = 0 


    output = get_body_str(app_type, cus_id)
    return response(output) 


#数据层
def get_body_str(app_type, cus_id):
    param = [int(app_type), cus_id]
    sql = '''
    select tb1.app_version, tb1.app_url, tb1.is_must from version_info tb1
    where tb1.app_type = %s 
    and tb1.app_switch = 1
    and tb1.cus_id = %s
    order by tb1.app_version desc 
    limit 1
    '''
    rsts = mysql.get(sql, param)
    #空值判断
    if rsts == None: return version_data.imedia_empty
    '''
    模板值替换
    用很简陋的方式读取模板
    '''
    template = version_data.imedia_body
    template = template.replace('{version}', str(rsts[0]))
    template = template.replace('{url}', const.download_domain + str(rsts[1]))
    template = template.replace('{is_must}', str(rsts[2]))
    return template
    
 
    
